import resourceManager from '@ohos.resourceManager';
import { Context } from '@kit.AbilityKit';

let rm: resourceManager.ResourceManager
let inited: boolean = false;

export function getResourceManager(): resourceManager.ResourceManager {
  return rm;
}

export function initResourceManager(context: Context) {
  if (!inited) {
    rm = context.resourceManager;
    inited = true;
  }
}

function getStringResourceContent(resId: number): string {
  return rm.getStringSync(resId);
}

export function getStringResourceContentByName(resName: string): string {
  return rm.getStringByNameSync(resName)
}

/**
 * 获取资源文件的内容.
 * 如果资源文件包含%d,%s等格式化字符串,可以在args中传值.
 * eg: getStringSync("xxx%s",sasdf)
 * @param r
 * @args
 * @returns
 */


export function $str(r: resourceManager.Resource, ...args: Array<number | string>): string {
  let id: number = r.id;
  return rm.getStringSync(id, ...args);
}

export function $int(r: resourceManager.Resource): number {
  let id: number = r.id;
  return rm.getNumber(id);
}

/**
 * 根据资源名称获取资源
 * eg: 获取资源字符串 $r('app.string.receive_folder')
 * 传入参数应为 ‘receive_folder’
 * @param resName
 * @returns
 */
export function getStringByName(resName: string, ...args: Array<string | number>): string {
  if (args) {
    return rm.getStringByNameSync(resName, ...args);
  } else {
    return rm.getStringByNameSync(resName)
  }
}

export function transStringArray(nameArray: Array<string>): string[] {
  return nameArray.map((value) => {
    return getStringByName(value)
  })
}

export const getRContent = $str;
